﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Media;

namespace mat2tab_fun.functionPlots
{
    class LogarythmicFunctionPlot : FunctionPlot
    {
        public LogarythmicFunctionPlot(bool testMonotonicznosci, Grid plotGrid, int size, int a, double b, int c)
            : base(testMonotonicznosci, plotGrid, size)
        {
            if (a == 0)
                return;
            double lastX = 0, lastY = 0;
            double delta = 0.05;
            // dodanie 1 punktu (0,niesk) lub (0, - niesk)
            Line fl = new Line();
            fl.X1 = halfX;
            if ((a < 0 && b > 1) || (a > 0 && b < 1)) // rosnaca
                fl.Y1 = -100000;
            else
                fl.Y1 = 100000;
            fl.X2 = delta * size + halfX;
            fl.Y2 = a * (Math.Log(c * delta) / Math.Log(b)) * size;
            if (fl.Y2 >= 0)
                fl.Y2 = halfY - fl.Y2;
            else
                fl.Y2 = fl.Y2 * (-1) + halfY;
            if (testMonotonicznosci)
            {
                if (fl.Y1 > fl.Y2)
                    fl.Stroke = Brushes.Red;
                else
                    fl.Stroke = Brushes.Green;
            }
            else
                fl.Stroke = Brushes.Blue;
            lines.Add(fl);

            for (double x = 0.05; x <= halfX / size; )
            {
                try
                {
                    Line l = new Line();
                    l.X1 = x * size + halfX;
                    l.Y1 = a * (Math.Log(c * x) / Math.Log(b)) * size;

                    if (l.Y1 >= 0)
                        l.Y1 = halfY - l.Y1;
                    else
                        l.Y1 = l.Y1 * (-1) + halfY;

                    x += delta;

                    l.X2 = x * size + halfX;
                    l.Y2 = a * (Math.Log(c * x) / Math.Log(b)) * size;

                    if (l.Y2 >= 0)
                        l.Y2 = halfY - l.Y2;
                    else
                        l.Y2 = l.Y2 * (-1) + halfY;
                    if (testMonotonicznosci)
                    {
                        if (l.Y1 > l.Y2)
                            l.Stroke = Brushes.Red;
                        else
                            l.Stroke = Brushes.Green;
                    }
                    else
                        l.Stroke = Brushes.Blue;
                    if (Math.Abs(l.Y1 - l.Y2) < 350)
                        lines.Add(l);

                    // jezeli jest to ostatni punkt to go zapamietujemy
                    if (x + delta > halfX)
                    {
                        lastX = l.X2;
                        lastY = l.Y2;
                    }
                }
                catch (Exception)
                { 
                
                }
            }
            DrawPlot();
        }
    }
}
